package com.yy.job.facade.converter;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.yy.job.common.enums.JobExecuteTypeEnum;
import com.yy.job.common.enums.JobStatusEnum;
import com.yy.job.common.enums.JobStepStatusEnum;
import com.yy.job.core.config.JobConfigXmlContext;
import com.yy.job.core.config.xml.JobStepXmlConfig;
import com.yy.job.core.config.xml.JobXmlConfig;
import com.yy.job.core.config.xml.ModuleXmlConfig;
import com.yy.job.core.domain.JobDomain;
import com.yy.job.core.domain.JobStepDomain;
import com.yy.job.facade.model.YYJobModel;
import com.yy.job.facade.model.YYJobStepModel;

@Component
public class YYJobModelConverter {
	
	@Resource
	private JobConfigXmlContext jobConfigParser;
	
	public JobDomain convertToDomain(YYJobModel jobModel) {
		// 获取xml配置信息
		ModuleXmlConfig findByModuleCode = this.jobConfigParser.getProjectXmlConfig()
				.findByModuleCode(jobModel.getModuleCode());
		JobXmlConfig jobConfig = findByModuleCode.findByJobCode(jobModel.getJobCode());

		// 组装job
		JobDomain jobDomain = new JobDomain();
		// projectCode值取xml中配置的值
		//jobDomain.setProjectCode(jobConfigParser.getProjectXmlConfig().getProjectCode());
		jobDomain.setProjectCode(jobModel.getProjectCode());
		jobDomain.setModuleCode(jobModel.getModuleCode());
		jobDomain.setModuleName(findByModuleCode.getModuleName());
		jobDomain.setJobCode(jobModel.getJobCode());
		jobDomain.setJobName(jobConfig.getJobName());
		
		// 创建立即执行模式和本地执行模式
		jobDomain.setJobExecuteType(jobModel.getJobExecuteType());
		if (JobExecuteTypeEnum.PRIORITY.equals(jobModel.getJobExecuteType()) 
			|| JobExecuteTypeEnum.LOCAL.equals(jobModel.getJobExecuteType())) {
			jobDomain.setJobStatus(JobStatusEnum.LOCK);
			jobDomain.setVersion(1);
		}
		else {
			jobDomain.setJobStatus(JobStatusEnum.CREATE);
			// 默认版本从0开始
			jobDomain.setVersion(0);
		}
		
		jobDomain.setExecCount(0);
		jobDomain.setBusinessNo(jobModel.getBusinessNo());
		// 设置期望启动时间
		jobDomain.setExpectedStartTime(jobModel.getExpectedStartTime());
		jobDomain.setOperator(jobModel.getOperator());
		
		// 组装steps
		for (YYJobStepModel stepDTO : jobModel.getSteps()) {
			JobStepXmlConfig stepXmlConfig = jobConfig.findJobStepConfig(stepDTO.getStepCode());
			JobStepDomain stepDomain = new JobStepDomain();
			stepDomain.setStepCode(stepDTO.getStepCode());
			// step名称可以被覆盖
			stepDomain.setStepName(stepXmlConfig.getStepName());
			if (StringUtils.isNotEmpty(stepDTO.getStepName())) {
				stepDomain.setStepName(stepDTO.getStepName());
			}
			stepDomain.setStepStatus(JobStepStatusEnum.CREATE);
			// order可以被覆盖
			if (stepDTO.getStepOrder() != null) {
				stepDomain.setStepOrder(stepDTO.getStepOrder());
			}
			else if (StringUtils.isNotBlank(stepXmlConfig.getStepOrder())) {
				stepDomain.setStepOrder(Integer.valueOf(stepXmlConfig.getStepOrder()));
				
			}
			stepDomain.setStepParams(stepDTO.getStepParams());
			stepDomain.setExecCount(0);
			
			stepDomain.setOperator(jobModel.getOperator());
			jobDomain.addJobStepDomain(stepDomain);
		}

		return jobDomain;
	}
	
}
